home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7820 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: rs6.iaee.tuwien.ac.at!sor
  2. From: sor@rs6.iaee.tuwien.ac.at (Evgeni Sorokin)
  3. Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland
  4. Subject: Re: Tough FACTORIAL math problem...
  5. Followup-To: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland
  6. Date: 19 Feb 1996 13:24:14 GMT
  7. Organization: Vienna University of Technology, Austria
  8. Message-ID: <4g9tlu$i9t@news.tuwien.ac.at>
  9. References: <4fr8be$ass@news.iconn.net>
  10. NNTP-Posting-Host: rs6.iaee.tuwien.ac.at
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13.  
  14. One may use the following observations: all zeros come from 5's and 2's, and
  15. there are more 2's than 5's.
  16.  
  17. Now, since last digit of product depend solely upon the last digits
  18. of the  multipliers, we keep only the last digits. We also collect
  19. all 2's separately, ahhihilating them with 5's, when possible.
  20.  
  21.  
  22. var LastDigit, Count, 
  23.     Multiplier, Twos : integer;
  24.  
  25. const Stop = 1000;
  26.  
  27. begin
  28.   LastDigit:=1; 
  29.   Twos:=0;
  30.   for Count:=1 to Stop do begin
  31.     While (Multiplier mod 2) = 0 do begin
  32.       Twos:=Twos+1;
  33.       Multiplier:=Multiplier div 2;
  34.     end;
  35.     While (Multiplier mod 5) = 0 do begin
  36.       Twos:=Twos-1;
  37.       Multiplier:=Multiplier div 5;
  38.     end;
  39.     LastDigit:=(LastDigit*Multiplier) mod 10;
  40.   end;
  41.   for Count:=1 to Twos do 
  42.     LastDigit:=(LastDigit*2) mod 10;
  43. end.
  44.  
  45.  
  46.  
  47.  
  48.  
  49. --
  50. Zhenya Sorokin
  51.  
  52. E-mail     : sorokin@ps1.iaee.tuwien.ac.at, sor@rs6.iaee.tuwien.ac.at
  53. Paper-mail : E. Sorokin, Gusshausstr. 27/359-4, 1040 Vienna, Austria
  54. Voice-mail : +43(1)58801-3703, -3948
  55. Fax-mail   : +43(1)504 2477
  56.